<?php


/* draws a calendar */
function draw_calendar($month,$year, $schedule_slot){

	/* draw table */
	$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';

	/* table headings */
	$headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
	$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

	/* days and weeks vars now ... */
	$running_day = date('w',mktime(0,0,0,$month,1,$year));
	$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
	$days_in_this_week = 1;
	$day_counter = 0;
	$dates_array = array();

	/* row for week one */
	$calendar.= '<tr class="calendar-row">';

	/* print "blank" days until the first of the current week */
	for($x = 0; $x < $running_day; $x++):
	$calendar.= '<td class="calendar-day-np">&nbsp;</td>';
	$days_in_this_week++;
	endfor;

	/* keep going with days.... */
	for($list_day = 1; $list_day <= $days_in_month; $list_day++):
	$calendar.= '<td class="calendar-day">';
	/* add in the day number */
	$calendar.= '<div class="day-number">'.$list_day.'</div>';

	$redlink = 'http://google.ca';
	$cal_note = $schedule_slot[$list_day];
	if( !$cal_note ) {
	    $words = "";
	} else { 
		$t1 = $cal_note->getTeam1Id();
		$t2 = $cal_note->getTeam2Id();
		$loc = $cal_note->getSport();
		$fac = $cal_note->getFacility();
		$tm = $cal_note->getHTime();
		$words = <<<Note
<b>$t1 vs. $t2<br></b>
<i>$loc ($fac: $tm)</i>
Note;
        }
	
	$calendar.= '<a href="' . $redlink . '"<div width="100" class="cal_box">' . $words . '</div></a>';

	
	$calendar.= '</td>';
	if($running_day == 6):
	$calendar.= '</tr>';
	if(($day_counter+1) != $days_in_month):
	$calendar.= '<tr class="calendar-row">';
	endif;
	$running_day = -1;
	$days_in_this_week = 0;
	endif;
	$days_in_this_week++; $running_day++; $day_counter++;
	endfor;

	/* finish the rest of the days in the week */
	if($days_in_this_week < 8):
	for($x = 1; $x <= (8 - $days_in_this_week); $x++):
	$calendar.= '<td class="calendar-day-np">&nbsp;</td>';
	endfor;
	endif;

	/* final row */
	$calendar.= '</tr>';

	/* end the table */
	$calendar.= '</table>';

	/* all done, return result */
	return $calendar;
}

/* sample usages */


$time = time();
$oldlocale = setlocale(LC_TIME, NULL); #save current locale
setlocale(LC_TIME, 'nl_NL'); #dutch
//echo generate_calendar(date('Y', $time), date('n', $time));


?>
<h2>
<?php 
if(!isset($_POST['month2'])  &&  !isset($_POST['month1'])){
	echo 'September 2011';

}else if(isset($_POST['month2'])){
	echo 'October 2011';
	
}else if(isset($_POST['month1'])){
	echo 'September 2011';
	//unset($_POST['month2']);
}
?>
</h2>

<form action="" method="post">
<input type="submit" name="month1" value="Prev Month"/>
</form>

<form action="" method="post">
<input type="submit" name="month2" value="Next Month"/>
</form>

<?php 
if(!isset($_POST['month2'])  &&  !isset($_POST['month1'])){
	echo draw_calendar('9','2011',$schedule_slot);
	//unset($_POST['month2']);
}else if(isset($_POST['month2'])){
	echo draw_calendar('10','2011',$schedule_slot);
	unset($_POST['month2']);
}else if(isset($_POST['month1'])){
	echo draw_calendar('9','2011',$schedule_slot);
	unset($_POST['month1']);
}

//echo draw_calendar(date('n',$time)+$_POST['monthval'], date('Y',$time), $schedule_slot);
setlocale(LC_TIME, $oldlocale);


?>


